package com.ktwlsoft.archivemanage.domain.business.repositoryService;

import com.ktwlsoft.archivemanage.domain.dao.entity.UnitMember;
import com.ktwlsoft.archivemanage.domain.dao.repository.UnitMemberMapper;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.unitMember.UnitMemberAddRequest;
import com.ktwlsoft.archivemanage.payload.unitMember.UnitMemberUpdateRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description     立档单位人员信息数据层服务类
 * @author          chenkun
 * @version         1.0
 * @date            2019-10-28
 */
@Service
public class UnitMemberRepositoryService {

    @Autowired
    private UnitMemberMapper unitMemberMapper;

    /**
     * @description 查询所有立档单位人员列表
     * @return Result
     */
    public Result findUnitMemberList(){
        Map<String,Object> data=new HashMap<>();
        return Result.body(unitMemberMapper.findUnitMemberList(data),ResultType.BUSINESS_SUCCESS);
    }

    /**
     * @description         查询立档单位人员信息
     * @param               unitId 立档单位id
     * @return              Result
     */
    public Result findUnitMemberInfoById(Long unitId){
        if(unitId == null){
            return Result.body(null, ResultType.BUSINESS_ERROR).withMessage("人员信息不存在");
        }
        return Result.body(unitMemberMapper.findUnitMemberInfoById(unitId), ResultType.BUSINESS_SUCCESS);
    }

    /**
     * @description         新增立档单位人员信息
     * @param               request 立档单位人员数据
     * @return              Result
     */
    public Result addUnitMemberInfo(UnitMemberAddRequest request){
//        Long unitId=request.getUnitId();
        UnitMember unitMember=new UnitMember();
        BeanUtils.copyProperties(request,unitMember);
//        if(null == unitId || "".equals(unitId)) {
//            return Result.body(null, ResultType.BUSINESS_ERROR).withMessage("立档单位信息不存在");
//        }
        try{
            unitMemberMapper.addUnitMemberInfo(unitMember);
        }catch (Exception e){
            e.printStackTrace();
            return Result.body(null,ResultType.SAVING_ERROR).withMessage(e.getMessage());
        }
        return Result.body(unitMember.getId(), ResultType.BUSINESS_SUCCESS).withMessage("新增人员信息成功");
    }

    /**
     * @description         新增立档单位人员信息
     * @param               unitMember 立档单位人员数据
     * @return              Result
     */
    public Result addUnitMemberInfo(UnitMember unitMember){
        try{
            unitMemberMapper.addUnitMemberInfo(unitMember);
        }catch (Exception e){
            e.printStackTrace();
            return Result.body(null,ResultType.SAVING_ERROR).withMessage(e.getMessage());
        }
        return Result.body(unitMember.getId(), ResultType.BUSINESS_SUCCESS).withMessage("新增人员信息成功");
    }

    /**
     * @description         删除立档单位人员信息
     * @param               unitId 立档单位id
     * @return              int
     */
    public int deleteUnitMemberInfo(Long unitId){
        return unitMemberMapper.deleteUnitMemberInfo(unitId);
    }

    /**
     * @description         更新立档单位人员信息
     * @param               request
     * @return              Result
     */
    public Result updateUintMemberInfo(UnitMemberUpdateRequest request){
//        Long unitId=request.getUnitId();
        UnitMember unitMember=new UnitMember();
//        if(unitId == null || "".equals(unitId)){
//            return Result.body(null, ResultType.BUSINESS_ERROR).withMessage("立档单位信息不存在");
//        }
        try{
            unitMemberMapper.updateUnitMemberInfo(unitMember);
        }catch (Exception e){
            e.printStackTrace();
            return Result.body(null,ResultType.UPDATE_ERROR).withMessage(e.getMessage());
        }
        return Result.body(null, ResultType.BUSINESS_SUCCESS).withMessage("更新立档单位人员信息成功");
    }

    /**
     * @description         更新立档单位人员信息
     * @param               unitMember
     * @return              Result
     */
    public Result updateUintMemberInfo(UnitMember unitMember){
        try{
            unitMemberMapper.updateUnitMemberInfo(unitMember);
        }catch (Exception e){
            e.printStackTrace();
            return Result.body(null,ResultType.UPDATE_ERROR).withMessage(e.getMessage());
        }
        return Result.body(null, ResultType.BUSINESS_SUCCESS).withMessage("更新立档单位人员信息成功");
    }

    /**
     * 查询列表 on 2019-10-24 14:19:45
     * @param unitMember unitMember
     * @return List<UnitMember>
     */
    public List<UnitMember> findList(UnitMember unitMember){
        return unitMemberMapper.findList(unitMember);
    }

    /**
     * 根据立档ID查询信息 on 2019-10-24 14:19:42
     * @param unitId	unitId
     * @return Infrastructure
     */
    public UnitMember getByUnitId(Long unitId){
        return unitMemberMapper.getByUnitId(unitId);
    }

    /**
      * 修改 on 2019/11/1 15:54
      *
      * @param unitMember unitMember
      * @return int
      */
    public int updateByPrimaryKeySelective(UnitMember unitMember){
        return unitMemberMapper.updateByPrimaryKeySelective(unitMember);
    }

    public UnitMember findUnitMemberByArchivistId(Long archivistId) {
        return unitMemberMapper.findUnitMemberByArchivistId(archivistId);
    }
}
